package com.zxisl.init;

import com.zxisl.bean.Mail;
import com.zxisl.util.XpathUtil;
import org.apache.commons.io.FileUtils;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.log4j.Logger;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.zxisl.util.XpathUtil.getListNodes;

/**
 * Created by ade on 2016/11/9.
 */
public class Init {

    public static Logger initLogger = Logger.getLogger(Init.class);
    public static List<String> subList = getSubjectByXpath();
    public static Map<String, Mail> mailMap = XpathUtil.getMailByXpath();
    public static KafkaProducer kafkaProducer = getKafkaProducer();
    public static List<String> proxyIpList = getProxyIp();
    public static String proxyStr = getProxyStr();
    private static Init init;
    private Init() {

    }

    public static Init getInit() {
        if (null == init) {
            return new Init();
        } else {
            return init;
        }

    }

    /**
     * 获取资源路径
     *
     * @return
     */
    public static String getResourcePath() {
        URL uri = getInit().getClass().getProtectionDomain().getCodeSource().getLocation();
        String str = uri.toString();
        return str.substring(0, str.lastIndexOf("/")).substring("file:".length());
    }

    /**
     * 通过xPath 文件名获取 elementList
     *
     * @param xPath
     * @param fileName
     * @return
     */
    public static List<Element> getElementList(String xPath, String fileName) {
        try {
            String str = getResourcePath() + "/conf/" + fileName;
            List<Element> elementList = new SAXReader().read(new File(str)).selectNodes(xPath);
            return elementList;
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 通过xPath 文件名获取element
     *
     * @param xPath
     * @param fileName
     * @return
     */
    public static Element getElement(String xPath, String fileName) {

        try {
            String str = getResourcePath() + "/conf/" + fileName;
            Element element = (Element) new SAXReader().read(new File(str)).selectSingleNode(xPath);
            return element;
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 获取proxyIp的路径名
     * @return
     */
    public static String getProxyStr(){
        List<String> list = new ArrayList<String>();
        String fileName = "proxyIp.txt";
        String str = getResourcePath() + "/conf/" + fileName;
        return str;
    }

    /**
     * 获取所有的socketProxyIp
     *
     * @return
     */
    public static List<String> getProxyIp() {
        List<String> list = new ArrayList<String>();
        String fileName = "proxyIp.txt";
        String str = getResourcePath() + "/conf/" + fileName;
        try {
            list = FileUtils.readLines(new File(str), Charset.defaultCharset());
            return list;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取kafka生产者对象
     *
     * @return
     */
    public static KafkaProducer getKafkaProducer() {
        try {
            String xPath = "/root";
            String fileName = "kafka.xml";
            Element dbElement = getElement(xPath, fileName);
            List<String> kafkaConfig = XpathUtil.getListNodes(dbElement);
            Map<String, Object> confMap = new HashMap<String, Object>();
            confMap.put("bootstrap.servers", kafkaConfig.get(0));
            confMap.put("key.serializer", kafkaConfig.get(1));
            confMap.put("value.serializer", kafkaConfig.get(2));
            KafkaProducer kafkaProducer = new KafkaProducer(confMap);
            return kafkaProducer;
        } catch (Exception e) {
            initLogger.info(e.getMessage());
            return null;
        }
    }

    /**
     * 获取要删除邮件的标识列表
     *
     * @return
     */
    public static List<String> getSubjectByXpath() {
        try {
            String xPath = "/root";
            String fileName = "deleteInfo.xml";
            Element dbElement = getElement(xPath, fileName);
            return getListNodes(dbElement);
        } catch (Exception e) {
            initLogger.info(e.getMessage());
            return null;
        }
    }

    //获取所有mail的数据
    public static List<Mail> getMailListByXpath(String[] userInfo) throws DocumentException {
        String xPath = "/root/mail";
        String fileName = "infoMail.xml";
        List<Element> elementList = getElementList(xPath, fileName);
        List<Mail> mailList = new ArrayList<Mail>();
        for (Element element : elementList) {
            //获取这些文本
            String server = element.elementText("server");
            String host = element.elementText("host");
            String protocal = element.elementText("protocal");
            String port = element.elementText("port");
            String ssl = element.elementText("ssl");
            Mail mail = new Mail();
            String[] mailStr = {server, host, protocal, port, userInfo[0], userInfo[1], ssl};
            mail.setMail(mailStr);
            mailList.add(mail);
        }
        return mailList;
    }

}
